package cn.com.lms.business.exam.service;

import cn.com.lms.business.core.constant.ErrorEnum;
import cn.com.lms.business.exam.persistence.entity.Paper;
import cn.com.lms.business.exam.persistence.entity.PaperQuestion;
import cn.com.lms.business.exam.persistence.repository.PaperQuestionRepository;


import com.bnzj.core.persistence.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class PaperQuestionService extends BaseService<PaperQuestionRepository, PaperQuestion,Long> {

    List<Map> findPaperQuestionByPaperIdList(List<Long> paperIdList){
       return repository.findPaperQuestionByPaperIdList(paperIdList);
    }

    List<Map> findPaperScoreByPaperIdList(List<Long> paperIdList){
       return repository.findPaperScoreByPaperIdList(paperIdList);
    }

    public void  batchSave(Paper paper, List<PaperQuestion> paperQuestions ){
        if (!CollectionUtils.isEmpty(paperQuestions)) {
            paperQuestions = paperQuestions.stream().map(paperQuestion -> {
                    paperQuestion.setPaperId(paper.getId());
                    paperQuestion.setCreator(paper.getCreator());
                    paperQuestion.setCreateTime(paper.getCreateTime());
                    paperQuestion.setCreatorName(paper.getCreatorName());
                    paperQuestion.setIsDelete(false);
                    return paperQuestion;
            }).collect(Collectors.toList());
            super.saveAll(paperQuestions);
        }

    }



    public void deleteByPaperId(Long paperId) {
        repository.deleteByPaperId(paperId);
    }

}
